package com.cssy.yd.logic.util;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.servlet.http.HttpServletRequest;

import com.cssy.authority.entity.ScUserRolewhPlaceDO;
import com.cssy.authority.service.IScUserRolewhPlaceService;
import com.cssy.xxkj.basic.entity.BasicStorageInfoTestDO;
import com.cssy.xxkj.basic.entity.BasicWorkshopDO;
import com.plat.helper.SearchConditions;
import com.plat.helper.SearchResult;
import com.plat.util.SessionUtil;

public class SqlUtil {
	public static SqlUtil sqlUtil = new SqlUtil();
	
	/**
	 * 返回当前登录用户所能查看的所有仓库编码
	 * @param request
	 * @param basicRolePlaceService
	 * @param option sql语句字段名称(等于这些仓库编码的那个字段)
	 * @return
	 */
	public Map<String,Object> roleWhSqlFilter(HttpServletRequest request,IScUserRolewhPlaceService basicRolePlaceService,String option){
		
		List<String> roleList = SessionUtil.getUserRoles(request);//获取当前用户所有角色
		List<String> whCodeList = new ArrayList<String>(); //定义一个List存储当前用户所能看到的仓库编码
		Set<String> set=new HashSet<String>();
		SearchConditions scs = new SearchConditions();
		scs.addCondition("l.roleName", roleList);
		scs.addCondition("l.type", "WH");
		scs.setRemovedProperty(null);
		//scs.setOrderBy("l.resourcesName");
		scs.setOrderBy("l.resourcesCode");//因utf-8的中文排序问题，修改为编码排序 @modify huang.xuming [2013-10-08]
		SearchResult sr = basicRolePlaceService.findListByExample(scs);
		if(sr != null && sr.getResults() != null && sr.getResults().size() >= 1){
			
			@SuppressWarnings("unchecked")
			List<ScUserRolewhPlaceDO> rolePlaceList = sr.getResults();
			for(ScUserRolewhPlaceDO brp : rolePlaceList){
//				whCodeList.add(brp.getResourcescode());
				set.add(brp.getResourcescode());
			}
			whCodeList.addAll(set);
		}else{
			option = " 1 = 2 AND  " + option;
			whCodeList.add("ERROR : NO ROLEPLACE");
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("option", option);
		map.put("roleWhList",whCodeList);
		return map;
	}
	
	/**
	 * 获取当前用户可以查看的仓库
	 * @param request
	 * @param basicRolePlaceService
	 * @return
	 */
	public List<BasicStorageInfoTestDO> roleWH(HttpServletRequest request,IScUserRolewhPlaceService basicRolePlaceService){
		List<BasicStorageInfoTestDO> list=new ArrayList<BasicStorageInfoTestDO>();
		List<String> roleList = SessionUtil.getUserRoles(request);//获取当前用户所有角色
		SearchConditions scs = new SearchConditions();
		scs.addCondition("l.roleName", roleList);
		scs.addCondition("l.type", "WH");
		scs.setRemovedProperty(null);
		//scs.setOrderBy("l.resourcesName");
		scs.setOrderBy("l.resourcesCode");//因utf-8的中文排序问题，修改为编码排序 @modify huang.xuming [2013-10-08]
		SearchResult sr = basicRolePlaceService.findListByExample(scs);
		if(sr != null && sr.getResults() != null && sr.getResults().size() >= 1){
			
			@SuppressWarnings("unchecked")
			List<ScUserRolewhPlaceDO> rolePlaceList = sr.getResults();
			Set<String> set = new HashSet<String>();
			for(ScUserRolewhPlaceDO brp : rolePlaceList){
				if(null != brp.getResourcesname()){// --不显示无仓库名称（仓库被删除）的角色仓库 @modify huang.xuming [2013-07-23]
					BasicStorageInfoTestDO bswh=new BasicStorageInfoTestDO();
					bswh.setZd002Code(brp.getResourcescode());
					bswh.setZd003Name(brp.getResourcesname());
					bswh.setId(brp.getId());//modify 2014.10.9 盘点新增是增加盘点仓库Id liubo
					if (set.add(brp.getResourcescode()))
						list.add(bswh);
			       }
				}
			}
		return list;
	}
	
	
	/**
	 * 返回当前登录用户所能查看的所有车间编码
	 * @param request
	 * @param basicRolePlaceService
	 * @param option sql语句字段名称(等于这些仓库编码的那个字段)
	 * @return
	 */
	public Map<String,Object> roleShSqlFilter(HttpServletRequest request,IScUserRolewhPlaceService basicRolePlaceService,String option){
		
		List<String> roleList = SessionUtil.getUserRoles(request);//获取当前用户所有角色
		List<String> shCodeList = new ArrayList<String>(); //定义一个List存储当前用户所能看到的车间编码
		Set<String> set=new HashSet<String>();
		SearchConditions scs = new SearchConditions();
		scs.addCondition("l.roleName", roleList);
		scs.addCondition("l.type", "SH");
		scs.setRemovedProperty(null);
		//scs.setOrderBy("l.resourcesName");
		scs.setOrderBy("l.resourcesCode");//因utf-8的中文排序问题，修改为编码排序 @modify huang.xuming [2013-10-08]
		SearchResult sr = basicRolePlaceService.findShopListByExample(scs);
		if(sr != null && sr.getResults() != null && sr.getResults().size() >= 1){
			
			@SuppressWarnings("unchecked")
			List<ScUserRolewhPlaceDO> rolePlaceList = sr.getResults();
			for(ScUserRolewhPlaceDO brp : rolePlaceList){
//				whCodeList.add(brp.getResourcescode());
				set.add(brp.getResourcescode());
			}
			shCodeList.addAll(set);
		}else{
			option = " 1 = 2 AND  " + option;
			shCodeList.add("ERROR : NO ROLEPLACE");
		}
		
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("option", option);
		map.put("roleShList",shCodeList);
		return map;
	}
	
	
	
	/**
	 * 获取当前用户可以查看的车间
	 * @param request
	 * @param basicRolePlaceService
	 * @return
	 */
	public List<BasicWorkshopDO> roleSH(HttpServletRequest request,IScUserRolewhPlaceService basicRolePlaceService){
		List<BasicWorkshopDO> list=new ArrayList<BasicWorkshopDO>();
		List<String> roleList = SessionUtil.getUserRoles(request);//获取当前用户所有角色
		SearchConditions scs = new SearchConditions();
		scs.addCondition("l.roleName", roleList);
		scs.addCondition("l.type", "SH");
		scs.setRemovedProperty(null);
		//scs.setOrderBy("l.resourcesName");
		scs.setOrderBy("l.resourcesCode");//因utf-8的中文排序问题，修改为编码排序 @modify huang.xuming [2013-10-08]
		SearchResult sr = basicRolePlaceService.findShopListByExample(scs);
		if(sr != null && sr.getResults() != null && sr.getResults().size() >= 1){
			
			@SuppressWarnings("unchecked")
			List<ScUserRolewhPlaceDO> rolePlaceList = sr.getResults();
			Set<String> set = new HashSet<String>();
			for(ScUserRolewhPlaceDO brp : rolePlaceList){
				if(null != brp.getResourcesname()){// --不显示无仓库名称（仓库被删除）的角色仓库 @modify huang.xuming [2013-07-23]
					BasicWorkshopDO bswh=new BasicWorkshopDO();
					bswh.setZd001Code(brp.getResourcescode());
					bswh.setZd002Name(brp.getResourcesname());
					bswh.setId(brp.getId());//modify 2014.10.9 盘点新增是增加盘点仓库Id liubo
					if (set.add(brp.getResourcescode()))
						list.add(bswh);
			       }
				}
			}
		return list;
	}
}
